
import sys

import direct.directbase.DirectStart

from direct.task import Task

from pandac.PandaModules import CardMaker
from pandac.PandaModules import CollisionNode
from pandac.PandaModules import CollisionRay
from pandac.PandaModules import CollisionHandlerQueue
from pandac.PandaModules import CollisionTraverser
from pandac.PandaModules import CollisionPolygon
from pandac.PandaModules import Vec3
from pandac.PandaModules import Point3
from pandac.PandaModules import TransparencyAttrib
from pandac.PandaModules import MouseButton

base.setBackgroundColor(0.9, 0.9, 0.9)
#base.setBackgroundColor(0.1, 0.1, 0.1)
base.disableMouse()

base.camLens.setNearFar(1.0, 50.0)
base.camLens.setFov(45.0)

camera.setPos(0.0, -3.0, 3.0)
camera.lookAt(0.0, 0.0, 0.0)

decal = CardMaker("Card")
decal.setFrame(-1.0, 1.0, -1.0, 1.0)
#decal.setColor(0.1, 0.1, 0.5, 1.0)
#decal.setBackgroundColor(1.0, 0.0, 0.0)
decalNode = render.attachNewNode(decal.generate())
#decalNode.setTexture(loader.loadTexture("grid.png"))
#decalNode.setPos(0.0, 0.0, 0.0)
#decalNode.setColor(1.0, 0.0, 0.0)

interval = decalNode.hprInterval(10.0, Vec3(80.0, 0.0, 0.0))
interval.loop()

from whatever import *

D3 = True

if D3:
	setGlobalNode(decalNode)
else:
	decalNode.hide()
	setGlobalNode(aspect2d)

grid = CardMaker("Card")
grid.setFrame(-2.0, 2.0, -1.0, 1.0)
gridNode = aspect2d.attachNewNode(grid.generate())
gridNode.setTexture(loader.loadTexture("grid3.png"))
gridNode.setTransparency(TransparencyAttrib.MAlpha)
gridNode.hide()

resources = WhateverResources()

minglemangle = WhateverMingleMangle()

checkbox0 = WhateverCheckBox(resources, 0.0, 0.1, WHATEVER_AUTO, 0.09)
minglemangle.addWidget(checkbox0)
checkbox1 = WhateverCheckBox(resources, 0.0, 0.2, WHATEVER_AUTO, 0.09)
checkbox1.setDisplayState(DISPLAY_DISABLED)
minglemangle.addWidget(checkbox1)
checkbox2 = WhateverCheckBox(resources, 0.0, 0.3, WHATEVER_AUTO, 0.09)
minglemangle.addWidget(checkbox2)

button0 = WhateverButton(resources, 0.0, 0.4, WHATEVER_AUTO, 0.09)
button0.setText("Hello World")
minglemangle.addWidget(button0)
button1 = WhateverButton(resources, 0.0, 0.5, WHATEVER_AUTO, 0.09)
button1.setText("RIP")
button1.setDisplayState(DISPLAY_DISABLED)
minglemangle.addWidget(button1)
button2 = WhateverButton(resources, 0.0, 0.6, WHATEVER_AUTO, 0.09)
button2.setText("Hello Panda3D")
minglemangle.addWidget(button2)

slider0 = WhateverSlider(resources, 0.0, 0.7, WHATEVER_AUTO, 0.09)
minglemangle.addWidget(slider0)
slider1 = WhateverSlider(resources, 0.0, 0.8, WHATEVER_AUTO, 0.09)
minglemangle.addWidget(slider1)

lineedit0 = WhateverLineEdit(resources, 0.0, 0.9, WHATEVER_AUTO, 0.09)
lineedit0.setText("Hello LineEdit")
minglemangle.addWidget(lineedit0)

texture0 = WhateverTexture(resources, -1.0, -0.6, WHATEVER_AUTO, 0.5, loader.loadTexture("dusk-dawn.png"))
minglemangle.addWidget(texture0)
texture1 = WhateverTexture(resources, -1.0, +0.1, WHATEVER_AUTO, 0.5, loader.loadTexture("dusk-dawn.png"))
texture1.setDisplayState(DISPLAY_DISABLED)
minglemangle.addWidget(texture1)

def gridShowHide():
	if gridNode.isHidden():
		gridNode.show()
	else:
		gridNode.hide()

quad = CollisionPolygon(Point3(-1.0, 0.0, -1.0), Point3(-1.0, 0.0, 1.0), Point3(1.0, 0.0, 1.0), Point3(1.0, 0.0, -1.0))
quadNode = decalNode.attachNewNode(CollisionNode("CollisionPolygon"))
quadNode.node().addSolid(quad)

ray = CollisionRay()
rayNode = camera.attachNewNode(CollisionNode("CollisionRay"))
rayNode.node().addSolid(ray)

queue = CollisionHandlerQueue()
traverser = CollisionTraverser("CollisionTraverser")
traverser.addCollider(rayNode, queue)

def update(task):
	if D3:
		if base.mouseWatcherNode.hasMouse():
			renderMouse = base.mouseWatcherNode.getMouse()
			ray.setFromLens(base.camNode, renderMouse.getX(), renderMouse.getY())
			traverser.traverse(decalNode)
			if queue.getNumEntries() > 0:
				entry = queue.getEntry(0)
				point = entry.getSurfacePoint(decalNode)
				pressed = base.mouseWatcherNode.isButtonDown(MouseButton.one()) == 1
				x, y = point[0], point[2]
				minglemangle.mouseEvent(x, y, pressed)

	else:
		if base.mouseWatcherNode.hasMouse():
			renderMouse = base.mouseWatcherNode.getMouse()
			pressed = base.mouseWatcherNode.isButtonDown(MouseButton.one()) == 1
			aspect2Mouse = globalNode.getRelativePoint(render, (renderMouse[0], 0.0, renderMouse[1]))
			x, y = aspect2Mouse[0], aspect2Mouse[2]
			minglemangle.mouseEvent(x, y, pressed)

	return Task.cont

taskMgr.add(update, "Update")

base.buttonThrowers[0].node().setButtonUpEvent("button-up")
base.buttonThrowers[0].node().setButtonDownEvent("button-down")
base.buttonThrowers[0].node().setButtonRepeatEvent("button-repeat")

base.accept("escape", sys.exit)
base.accept("a", render.analyze)
base.accept("g", gridShowHide)

base.accept("button-up", minglemangle.keyReleaseEvent)
base.accept("button-down", minglemangle.keyPressEvent)
base.accept("button-repeat", minglemangle.keyPressEvent)

run()
